package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Discover_Recommend_HeadViewpagerAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_fragment.AlbumDetalFragment;
import com.qiaoyanqing.ximalaya.discovermodule.discover_fragment.AlbumShowFragment;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class DiscoverRecomentAlbumActivity extends AppCompatActivity  {
    private ViewPager malbumviewpager;
    private TabLayout malbumtablayout;
    private List<Fragment> mData;


    private ImageView albumimg,backlastindex;
    private TextView albumanchor,albumtitle,playcount,nickname;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.album_test);
        malbumtablayout= (TabLayout) findViewById(R.id.albumtablayout);
        malbumviewpager= (ViewPager) findViewById(R.id.albumviewpager);
        //初始化控件
        initView();

        initNetData();


        initData();

        initTabLayout();

        initViewpager();

        initListener();


    }

    private void initNetData() {
        Intent intent=getIntent();
        String  icpaths=intent.getStringExtra("iconpath");
        String  nicknames=intent.getStringExtra("nickname");
        String  title=intent.getStringExtra("title");
        int playcounts=intent.getIntExtra("playcounts",8414060);

        Picasso.with(this).load(icpaths).into(albumimg);
        nickname.setText(nicknames);
        albumtitle.setText(title);
        playcount.setText((int) (playcounts/10000)+"万次");

    }

    private void initView() {
        albumimg= (ImageView) findViewById(R.id.albumimg);
        albumanchor= (TextView) findViewById(R.id.albumanchor);
        albumtitle= (TextView) findViewById(R.id.albumtitle);
        playcount= (TextView) findViewById(R.id.playcount);
        nickname= (TextView) findViewById(R.id.nickname);
        backlastindex= (ImageView) findViewById(R.id.backlastindex);

    }



    private void initListener() {
        malbumviewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(malbumtablayout));
        malbumtablayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(malbumviewpager));

        backlastindex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void initViewpager() {
        Discover_Recommend_HeadViewpagerAdapter discoverRecommendHeadViewpagerAdapter = new Discover_Recommend_HeadViewpagerAdapter(getSupportFragmentManager(),mData);
        malbumviewpager.setAdapter(discoverRecommendHeadViewpagerAdapter);

    }

    private void initTabLayout() {
        String[] title = getResources().getStringArray(R.array.albumtitle);
        for (int i = 0; i < title.length; i++) {
            TabLayout.Tab tab = malbumtablayout.newTab();
            tab.setText(title[i]);
            malbumtablayout.addTab(tab);
        }
        malbumtablayout.setTabMode(TabLayout.MODE_FIXED);
        malbumtablayout.setTabTextColors(Color.BLACK, Color.RED);
    }


    private void initData() {
        mData=new ArrayList<>();

        AlbumDetalFragment albumDetalFragment=new AlbumDetalFragment();
        AlbumShowFragment albumShowFragment=new AlbumShowFragment();

        mData.add(albumDetalFragment);
        mData.add(albumShowFragment);


    }


}
